package com.elong.qadev.dao.impl;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.elong.qadev.action.LoginAction;
import com.elong.qadev.dao.UserDaoI;
import com.elong.qadev.entity.User;
/**
 * 
 * @author kangchen
 *
 */
@Repository("userDao")
public class UserDaoImpl implements UserDaoI {
	Logger logger = Logger.getLogger(LoginAction.class);

	/**
	 * 使用@Autowired注解将sessionFactory注入到UserDaoImpl中
	 */
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Serializable save(User user) {
		return sessionFactory.getCurrentSession().save(user);
	}

	@Override
	public User find(String mail) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				User.class);
		c.add(Restrictions.eq("mail", mail));
		User u = null;
		if(c.list().size()!=0&&c.list().get(0) != null){
			u = (User) c.list().get(0);			
			logger.debug("获取登陆用户为：" + u.toString());
		}
		if (u != null) {
			return u;
		}
		return u;
	}

	@Override
	public void updatePassword(User u) {
		User user = find(u.getMail());
		user.setPassword(u.getPassword());
		sessionFactory.getCurrentSession().update(user);
	}

	@Override
	public void updatePhoneNumber(User u) {
		User user = find(u.getMail());
		user.setPhoneNumber(u.getPhoneNumber());
		sessionFactory.getCurrentSession().update(user);
	}

}